package com.hhit.bigscreenbackmanage.service.impl.zlgl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.dao.zlgl.TZlglMonthlyMeetingMapper;
import com.hhit.bigscreenbackmanage.entity.zlgl.TZlglMonthlyMeeting;
import com.hhit.bigscreenbackmanage.service.impl.jsgl.TJsqlTysjBdzServiceImpl;
import com.hhit.bigscreenbackmanage.service.zlgl.TZlglMonthlyMeetingService;
import com.hhit.bigscreenbackmanage.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TZlglMonthlyMeetingServiceImpl implements TZlglMonthlyMeetingService {

    private Logger log = LoggerFactory.getLogger(TJsqlTysjBdzServiceImpl.class);

    @Autowired
    private TZlglMonthlyMeetingMapper tZlglMonthlyMeetingMapper;

    @Override
    public JSONObject getTZlglMonthlyMeetingAll(JSONObject json)throws Exception {
        if (json.get("dataTime") == null || "".equals(json.get("dataTime"))){
            new Exception("需要数据的日期为空！");
        }
        JSONObject job = tZlglMonthlyMeetingMapper.getTZlglMonthlyMeetingAll(json);
        String indexAnalysis = job.get("indexAnalysis").toString();
        String managementWork = job.get("managementWork").toString();
        String voltageLevel = job.get("voltageLevel").toString();

        List<String> Analysis = Utils.getChildMsgByParent(indexAnalysis,"<<",">>");
        Map<String,Object> AnalysisMap = new LinkedHashMap();
        for (int i = 0; i < Analysis.size(); i++){
//            Analysis.get(i).replace("<<","").replace(">>","");
            AnalysisMap.put(Analysis.get(i).replace("<<","").replace(">>",""),Analysis.get(i+1).replace("<<","").replace(">>",""));
            ++i;
        }
//        job.put("indexAnalysis",AnalysisMap);
        job.put("indexAnalysis",AnalysisMap);
        List<String> Work = Utils.getChildMsgByParent(managementWork,"<<",">>");
        Map<String,Object> WorkMap = new LinkedHashMap();
        for (int i = 0; i < Work.size(); i++){
//            Work.get(i).replace("<<","").replace(">>","");
            WorkMap.put(Work.get(i).replace("<<","").replace(">>",""),Work.get(i+1).replace("<<","").replace(">>",""));
            ++i;
        }
//        job.put("managementWork",Work);
        job.put("managementWork",WorkMap);
        List<String> Level = Utils.getChildMsgByParent(voltageLevel,"<<",">>");
        Map<String,Object> LevelMap = new LinkedHashMap();
        for (int i = 0; i < Level.size(); i++){
//            Level.get(i).replace("<<","").replace(">>","");
            LevelMap.put(Level.get(i).replace("<<","").replace(">>",""),Level.get(i+1).replace("<<","").replace(">>",""));
            ++i;
        }
//        job.put("voltageLevel",Level);
        job.put("voltageLevel",LevelMap);
        return job;
    }

    @Override
    public Map<String, Object> insertTZlglMonthlyMeeting(JSONObject json) throws Exception {
        Map<String,Object> resultMap = new HashMap<>();
        int result = 0;
        JSONObject meeting = tZlglMonthlyMeetingMapper.getTZlglMonthlyMeetingAll(json);
        if (meeting == null){
            json.put("id",UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
            result =  tZlglMonthlyMeetingMapper.insertTZlglMonthlyMeeting(json);
        }else {
            result =  tZlglMonthlyMeetingMapper.updateTZlglMonthlyMeeting(json);
        }
        if (result > 0){
            resultMap.put("flag",true);
        }else {
            resultMap.put("flag",false);
        }
        return resultMap;
    }
}
